import { PiniaPluginContext } from 'pinia'

export default ({ store }: PiniaPluginContext) => {
  // init state from localstorage
  const lsKey = store.prefix + store.$id
  const lsData = localStorage.getItem(lsKey)
  if (lsData) {
    store.$state = JSON.parse(lsData)
  }

  store.$subscribe(() => {
    // update localstorage
    localStorage.setItem(lsKey, JSON.stringify(store.$state))
  })
}
